package com.ruoyi.business.service.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.business.mapper.WxUserMapper;
import com.ruoyi.business.domain.WxUser;
import com.ruoyi.business.service.IWxUserService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;

/**
 * 微信小程序用户Service业务层处理
 *
 * @author ruoyi
 */
@Service
public class IWxUserServiceImpl implements IWxUserService
{
    private static final Logger log = LoggerFactory.getLogger(IWxUserServiceImpl.class);

    @Autowired
    private WxUserMapper wxUserMapper;

    /**
     * 查询微信小程序用户列表
     *
     * @param wxUser 微信小程序用户
     * @return 微信小程序用户
     */
    @Override
    public List<WxUser> selectWxUserList(WxUser wxUser)
    {
        return wxUserMapper.selectWxUserList(wxUser);
    }

    /**
     * 查询微信小程序用户
     *
     * @param userId 微信小程序用户ID
     * @return 微信小程序用户
     */
    @Override
    public WxUser selectWxUserById(Long userId)
    {
        if (userId == null)
        {
            throw new ServiceException("用户ID不能为空");
        }
        return wxUserMapper.selectWxUserById(userId);
    }

    /**
     * 根据openid查询微信小程序用户
     *
     * @param openid 微信openid
     * @return 微信小程序用户
     */
    @Override
    public WxUser selectWxUserByOpenid(String openid)
    {
        if (StringUtils.isEmpty(openid))
        {
            throw new ServiceException("openid不能为空");
        }
        return wxUserMapper.selectWxUserByOpenid(openid);
    }

    /**
     * 新增微信小程序用户
     *
     * @param wxUser 微信小程序用户
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWxUser(WxUser wxUser)
    {
        if (wxUser == null)
        {
            throw new ServiceException("用户信息不能为空");
        }

        if (StringUtils.isEmpty(wxUser.getOpenid()))
        {
            throw new ServiceException("openid不能为空");
        }

        // 检查openid是否已存在
        WxUser existUser = wxUserMapper.selectWxUserByOpenid(wxUser.getOpenid());
        if (existUser != null)
        {
            throw new ServiceException("该openid已存在");
        }

        // 设置默认值
        if (wxUser.getStatus() == null)
        {
            wxUser.setStatus("0");
        }
        wxUser.setCreateTime(DateUtils.getNowDate());

        try
        {
            return wxUserMapper.insertWxUser(wxUser);
        }
        catch (Exception e)
        {
            log.error("新增微信用户失败", e);
            throw new ServiceException("新增微信用户失败");
        }
    }

    /**
     * 修改微信小程序用户
     *
     * @param wxUser 微信小程序用户
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWxUser(WxUser wxUser)
    {
        if (wxUser == null)
        {
            throw new ServiceException("用户信息不能为空");
        }

        if (wxUser.getUserId() == null)
        {
            throw new ServiceException("用户ID不能为空");
        }

        // 检查用户是否存在
        WxUser existUser = wxUserMapper.selectWxUserById(wxUser.getUserId());
        if (existUser == null)
        {
            throw new ServiceException("用户不存在");
        }

        // 如果修改了openid，检查新openid是否已存在
        if (StringUtils.isNotEmpty(wxUser.getOpenid()) && !wxUser.getOpenid().equals(existUser.getOpenid()))
        {
            WxUser userByOpenid = wxUserMapper.selectWxUserByOpenid(wxUser.getOpenid());
            if (userByOpenid != null)
            {
                throw new ServiceException("该openid已被其他用户使用");
            }
        }

        wxUser.setUpdateTime(DateUtils.getNowDate());

        try
        {
            return wxUserMapper.updateWxUser(wxUser);
        }
        catch (Exception e)
        {
            log.error("修改微信用户失败", e);
            throw new ServiceException("修改微信用户失败");
        }
    }

    /**
     * 删除微信小程序用户信息
     *
     * @param userId 微信小程序用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWxUserById(Long userId)
    {
        if (userId == null)
        {
            throw new ServiceException("用户ID不能为空");
        }

        // 检查用户是否存在
        WxUser existUser = wxUserMapper.selectWxUserById(userId);
        if (existUser == null)
        {
            throw new ServiceException("用户不存在");
        }

        try
        {
            return wxUserMapper.deleteWxUserById(userId);
        }
        catch (Exception e)
        {
            log.error("删除微信用户失败", e);
            throw new ServiceException("删除微信用户失败");
        }
    }

    /**
     * 批量删除微信小程序用户信息
     *
     * @param userIds 需要删除的微信小程序用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWxUserByIds(Long[] userIds)
    {
        if (userIds == null || userIds.length == 0)
        {
            throw new ServiceException("用户ID不能为空");
        }

        try
        {
            return wxUserMapper.deleteWxUserByIds(userIds);
        }
        catch (Exception e)
        {
            log.error("批量删除微信用户失败", e);
            throw new ServiceException("批量删除微信用户失败");
        }
    }
}